# -*- coding: utf-8 -*-

import pandas as pd
from models.student import Student

class FileManager:
    def __init__(self, db):
        self.db = db

    def import_from_excel(self, file_path, exam_id):
        df = pd.read_excel(file_path)
        # print("Excel文件的列名： ", df.columns.tolist()) # 打印列名
        required_columns = [
            '姓名', '班级', '总分(分数)', '总分(校序)', '总分(班序)',
            '语文(分数)', '语文(校序)', '语文(班序)',
            '数学(分数)', '数学(校序)', '数学(班序)',
            '外语(分数)', '外语(校序)', '外语(班序)',
            '物理(分数)', '物理(校序)', '物理(班序)',
            '化学(分数)', '化学(校序)', '化学(班序)',
            '生物(分数)', '生物(校序)', '生物(班序)'
        ]
        if not all(column in df.columns for column in required_columns):
            raise ValueError(f"Excel 文件必须包含以下列: {required_columns}")
        for _, row in df.iterrows():
            student = Student(
                exam_id = exam_id,
                name=row['姓名'],
                class_name=row['班级'],
                total_score=row['总分(分数)'],
                total_school_rank=row['总分(校序)'],
                total_class_rank=row['总分(班序)'],
                chinese_score=row['语文(分数)'],
                chinese_school_rank=row['语文(校序)'],
                chinese_class_rank=row['语文(班序)'],
                math_score=row['数学(分数)'],
                math_school_rank=row['数学(校序)'],
                math_class_rank=row['数学(班序)'],
                english_score=row['外语(分数)'],
                english_school_rank=row['外语(校序)'],
                english_class_rank=row['外语(班序)'],
                physics_score=row['物理(分数)'],
                physics_school_rank=row['物理(校序)'],
                physics_class_rank=row['物理(班序)'],
                chemistry_score=row['化学(分数)'],
                chemistry_school_rank=row['化学(校序)'],
                chemistry_class_rank=row['化学(班序)'],
                biology_score=row['生物(分数)'],
                biology_school_rank=row['生物(校序)'],
                biology_class_rank=row['生物(班序)']
            )
            self.db.insert_student(student)
